﻿$(function () {
    var url = new Url();
    var $css = url.GetQueryString("css");

    selTemplateBindData();//绑定选择模板下拉菜单信息
    selProductTypeBindData();//绑定产品类型下拉菜单信息

    //根据条件查询信息
    $(".btnSearch").bind("click", function () {
        var $CusType = ""//客户类型：理财or借款
        if ($('input:radio[name=CustomerType]')[0].checked) {
            $CusType = "理财";
        }
        else {
            $CusType = "借款";
        }
        var $ProType = $(".selProductType").val();//获取产品类型信息值
        var $StartTime = $(".StartDate").val();//合同开始日期
        var $EndTime = $(".EndDate").val();//合同结束日期
        var $Name = $(".txtName").val();//姓名
        var $IDCard = $(".txtIdCard").val();//身份证号
        $.ajax({
            type: "post",
            url: "../Handler/MailSendHandler.ashx",
            dataType: "json",
            data: { type: 3, cusType: $CusType, proType: $ProType, startTime: $StartTime, endTime: $EndTime, name: $Name, iDCard: $IDCard },
            async: false,
            cache: true,
            success: function (data) {
                if (data.Status) {
                    var name = "";
                    var j = 0;
                    $("#dataList").empty();
                    $(data.Data).each(function (i, e) {
                        $("#UserInfoListTemplete").tmpl(e).appendTo("#dataList");
                        if (i == 0 || i == 1) {
                            name += e.Name + ",";
                        }
                        j++;
                    });
                    if (name != "") {
                        if (j > 2) {
                            name = name.substr(0, name.length - 1) + "...";
                        }
                        else {
                            name = name.substr(0, name.length - 1);
                        }
                    }
                    $("#txtOptId").val(name);
                }
                else {
                    $("#dataList").empty();
                    $("#txtOptId").val("");
                }
            }
        });
    });

    //选择模板名称时
    $(".selTemplate").change(function () {
        var $MailTempId = $(".selTemplate").val();
        if ($MailTempId == -1)
        {
            $("#txtMessageContent").val("");
            alert("请选择模板项");
            return;
        }
        $.ajax({
            type: "post",
            url: "../Handler/MailSendHandler.ashx",
            dataType: "json",
            data: { type: 4, MailTempId: $MailTempId },
            async: false,
            cache: true,
            success: function (data) {
                if (data.Status) {
                    $("#txtMessageContent").empty();
                    $(data.Data).each(function (i, e) {
                        $("#txtMessageContent").val(e.MailTemplateContent);
                    });
                }
            }
        });
    });

    //发送邮件
    $(".btnSendClass").bind("click", function () {
        if ($("#txtOptId").val() == "") {
            alert("请选择接收人");
            return;
        }
        if ($(".selTemplate").val() == -1) {
            alert("请选择模板");
            return;
        }
        var $CusType = ""//客户类型：理财or借款
        if ($('input:radio[name=CustomerType]')[0].checked) {
            $CusType = "理财";
        }
        else {
            $CusType = "借款";
        }
        var $ProType = $(".selProductType").val();//获取产品类型信息值
        var $StartTime = $(".StartDate").val();//合同开始日期
        var $EndTime = $(".EndDate").val();//合同结束日期
        var $Name = $(".txtName").val();//姓名
        var $IDCard = $(".txtIdCard").val();//身份证号
        var $MailContent = $("#txtMessageContent").val();
        $.ajax({
            type: "post",
            url: "../Handler/MailSendHandler.ashx",
            dataType: "json",
            data: { type: 5, MailContent: $MailContent, cusType: $CusType, proType: $ProType, startTime: $StartTime, endTime: $EndTime, name: $Name, iDCard: $IDCard },
            async: false,
            cache: true,
            success: function (data) {
                if (data.Status) {
                    alert(data.Message);
                }
            }
        });
    });
});


//绑定选择模板下拉菜单信息
function selTemplateBindData() {
    $.ajax({
        type: "post",
        url: "../Handler/MailSendHandler.ashx",
        dataType: "json",
        data: { type: 1 },
        async: false,
        cache: true,
        success: function (data) {
            if (data.Status) {
                var str = '<option value="-1">请选择</option>';
                $(data.Data).each(function (i, e) {
                    str += '<option value="' + e.MailTemplateId + '">' + e.MailTemplateName + '</option>';
                });
                $(".selTemplate").append(str);
            }
        }
    });
}

//绑定产品类型下拉菜单信息
function selProductTypeBindData() {
    $.ajax({
        type: "post",
        url: "../Handler/MailSendHandler.ashx",
        dataType: "json",
        data: { type: 2 },
        async: false,
        cache: true,
        success: function (data) {
            if (data.Status) {
                var str = '<option value="-1">请选择</option>';
                $(data.Data).each(function (i, e) {
                    str += '<option value="' + e.ProductId + '">' + e.ProductName + '</option>';
                });
                $(".selProductType").append(str);
            }
        }
    });
}
